package acwing._2LanQiao._8NumberTheory;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._2LanQiao._8numberTheory
 * @File : Q1301.java
 * @Author : WangRuoyu
 * @Date : 2023/3/22 17:27
 */

import java.io.IOException;
import java.util.Scanner;

public class Q1301 {

    static long exgcd(long a, long b, long[] x, long[] y) {
        if (b == 0) {
            x[0] = 1;
            y[0] = 0;
            return a;
        }
        long d = exgcd(b, a % b, y, x);
        y[0] -= a / b * x[0];
        return d;
    }

    public static void main(String[] args) throws IOException {
//        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        String line = br.readLine();
//        String[] words = line.split(" ");
//        long a = Long.parseLong(words[0]);
//        long b = Long.parseLong(words[1]);
//        long c = Long.parseLong(words[2]);
//        long k = Long.parseLong(words[3]);

        Scanner scan = new Scanner(System.in);
        long a = scan.nextLong();
        long b = scan.nextLong();
        long c = scan.nextLong();
        long k = scan.nextLong();

        while (a != 0 || b != 0 || c != 0 || k != 0) {

            long circle = 1L << k;
            long dis = (b - a + circle) % circle;

            long[] x = {0};
            long[] y = {0};

            long d = exgcd(c, circle, x, y);
            if (dis % d != 0) {
                System.out.println("FOREVER");
            } else {
                long circle_ = circle / d;
                x[0] *= (dis / d);
                x[0] = (x[0] % circle_ + circle_) % circle_;
                System.out.println(x[0]);
            }
            a = scan.nextLong();
            b = scan.nextLong();
            c = scan.nextLong();
            k = scan.nextLong();
        }
    }
}